package com.server.http.controller;

import com.server.dao.MobileAppDao;
import com.server.dao.MobileAppUnitDao;
import com.server.dao.UnitDao;
import com.server.pojo.Alarm;
import com.server.pojo.MobileApp;
import com.server.pojo.MobileAppUnit;
import com.server.pojo.Unit;
import com.server.http.anYuan.AnYuanServer;
import com.server.http.service.MobileAppService;
import com.server.http.service.UnitService;
import com.server.libs.ret.RE;
import com.server.libs.ret.R;
import com.server.libs.login.AppAuth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping(value = "/unit")
@Slf4j
@Transactional
public class UnitController {
    @Autowired
    UnitDao unitDao;
    @Autowired
    MobileAppDao mobileAppDao;

    @Autowired
    StringRedisTemplate redis;

    @Autowired
    UnitService unitService;

    @Autowired
    MobileAppService mobileAppService;

    @Autowired
    MobileAppUnitDao mobileAppUnitDao;

    @Autowired
    AppAuth appAuth;


    @RequestMapping(value = "/all",method = {RequestMethod.GET,RequestMethod.POST})
    public R<List<Unit>> all(@RequestHeader String authorization){
        List<Unit> units = unitDao.findByIsCanJoin(true);
        checkJoin(units,authorization);
        return new R<List<Unit>>("获取成功").setData(units);
    }


    @RequestMapping(value = "/id",method = {RequestMethod.GET,RequestMethod.POST})
    public R<Unit> unitId(@RequestHeader String authorization, @RequestBody Map map){
        Integer unitId = (Integer) map.get("unitId");
        Unit unit = unitService.checkId(unitId);
        if(unit == null) throw new RE("找不到该单位");

        checkJoin(unit,authorization);
        return new R<Unit>("获取成功").setData(unit);
    }

    @RequestMapping(value = "/code",method = {RequestMethod.GET,RequestMethod.POST})
    public R<Unit> unitCode(@RequestHeader String authorization, @RequestBody Map map){
        String code = (String) map.get("code");
        Unit unit = unitDao.findByCode(code);
        if(unit == null) throw new RE("找不到该单位");

        checkJoin(unit,authorization);
        return new R<Unit>("获取成功").setData(unit);
    }


    @RequestMapping(value = "/alarms",method = {RequestMethod.GET,RequestMethod.POST})
    public R<Set<Alarm>> alarms(@RequestHeader String authorization, @RequestBody Map map){
        Integer unitId = (Integer) map.get("unitId");
        Unit unit = unitService.checkId(unitId);
        Set<Alarm> alarms = unit.getAlarms();
        for (Alarm alarm : alarms) {
            alarm.setUnit(unit); /*重新设置 减少查询*/
        }

        return new R<Set<Alarm>>("获取成功").setData(alarms);
    }


    @RequestMapping(value = "/join",method = {RequestMethod.GET,RequestMethod.POST})
    public R<String> bindUnit(@RequestHeader String authorization, @RequestBody Map map){
        Integer id = (Integer) map.get("id");
        String code = (String) map.get("code");

        if(id == null && code == null) {
            throw new RE("请输入单位ID(编号)");
        }
        /* 检查登录 */
        MobileApp mobileApp = mobileAppService.checkLogin(authorization);

        /* 检查单位 */
        Unit unit;
        if(id != null) {
            unit = unitService.checkId(id);
        } else {
            unit = unitService.checkCode(code);
        }

        if (unit.getIsCanJoin() != null && !unit.getIsCanJoin()){
            throw new RE("该单位不可加入");
        }
        unitService.checkNotJoin(unit,mobileApp);
        AnYuanServer.bindUnit(mobileApp,unit);
        mobileAppUnitDao.save(new MobileAppUnit(mobileApp, unit));
        return new R<>("绑定成功");
    }

    @RequestMapping(value = "/quit",method = {RequestMethod.GET,RequestMethod.POST})
    public R<String> quitUnit(@RequestHeader String authorization, @RequestBody Map map){
        Integer id = (Integer) map.get("id");
        String code = (String) map.get("code");


        if(id == null && code == null) {
            throw new RE("请输入单位ID(编号)");
        }
        /* 检查登录 */
        MobileApp mobileApp = mobileAppService.checkLogin(authorization);

        /* 检查单位 */
        Unit unit;
        if(id != null) {
            unit = unitService.checkId(id);
        } else {
            unit = unitService.checkCode(code);
        }

        /* 检查加入 */
        MobileAppUnit bind = unitService.checkJoin(unit,mobileApp);

        AnYuanServer.unBindUnit(mobileApp,unit);
        mobileAppUnitDao.delete(bind);

        return new R<>("退出成功");
    }


    void checkJoin(List<Unit> units, String auth){
        MobileApp mobileApp = mobileAppService.get(auth);
        if(mobileApp != null){
            List<Unit> units1 = mobileAppUnitDao.getUnits(mobileApp);
            units.forEach(var->{
                units1.forEach(var1->{
                    if (var.equals(var1))var.setIsJoin(true);
                });
            });
        }
    }

    void checkJoin(Unit unit, String auth){
        MobileApp mobileApp = mobileAppService.get(auth);
        if(mobileApp != null){
            List<Unit> units = mobileAppUnitDao.getUnits(mobileApp);
            units.forEach(var->{
                if (var.equals(unit))unit.setIsJoin(true);
            });
        }
    }

}
